{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "VegVdI5SakPo"
   },
   "source": [
    "## Generating Price Data using Stocastic Processes\n",
    "\n",
    "*   Geometric Brownian Motion\n",
    "*   The Merton Jump-Diffusion Model\n",
    "*   The Heston Stochastic Volatility Model\n",
    "*   Cox Ingersoll Ross (CIR)\n",
    "*   Ornstein Uhlebneck stochastic process\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "Sb1ah4EalN1Y"
   },
   "source": [
    "#### Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "C89HmbASlM16"
   },
   "outputs": [],
   "source": [
    "from tensortrade.exchanges.simulated.stochastic_exchange import StochasticExchange\n",
    "from tensortrade.exchanges.simulated.stochastic.model_params import ModelParameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "_U03fdj2g138"
   },
   "outputs": [],
   "source": [
    "import random\n",
    "import matplotlib.pyplot as plt\n",
    "import plotly.graph_objects as go\n",
    "import pandas as pd\n",
    "from datetime import datetime"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "2UgwTV1fbrnj"
   },
   "source": [
    "#### GRAPHING THE RESULTS"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "Rnce4Y_3bz2y"
   },
   "source": [
    "##### Matplotlib (Plotting the raw data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "WagfgXvxbvPv"
   },
   "outputs": [],
   "source": [
    "def plot_stochastic_processes(data, title):\n",
    "    \"\"\"\n",
    "    This method plots a list of stochastic processes with a specified title\n",
    "    :return: plots the graph of the two\n",
    "    \"\"\"\n",
    "    fig = go.Figure()\n",
    "    fig.add_trace(go.Scatter(x=data.index, y=data['close'], mode='lines', name='Prices'))\n",
    "    fig.update_layout(title=title,\n",
    "                    xaxis_title='TimeStep',\n",
    "                    yaxis_title='Price')\n",
    "    fig.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "YmEqjdDBb3pL"
   },
   "source": [
    "##### Plotly (Plotting ohlcv)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "Q5aXb-GGb5w_"
   },
   "outputs": [],
   "source": [
    "def plot_ohlcv(data_frame):\n",
    "\n",
    "    fig = go.Figure(data=[go.Candlestick(x=data_frame.index,\n",
    "                    open=data_frame.open,\n",
    "                    high=data_frame.high,\n",
    "                    low=data_frame.low,\n",
    "                    close=data_frame.close)])\n",
    "    fig.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "BURv7GmupiC4"
   },
   "source": [
    "### Generating Stochastic Data\n",
    "Default FBM Generated Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "JvAza4bsS9WM"
   },
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'StochasticExchange' object has no attribute '_model_type'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-5-e1da2c0f3052>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mexchange\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mStochasticExchange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m/usr/local/lib/python3.7/site-packages/tensortrade/base/component.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(cls, *args, **kwargs)\u001b[0m\n\u001b[1;32m     19\u001b[0m         \u001b[0minstance\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcls\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__new__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcls\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     20\u001b[0m         \u001b[0msetattr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minstance\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'context'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mContext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0mconfig\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 21\u001b[0;31m         \u001b[0minstance\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__init__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     22\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     23\u001b[0m         \u001b[0;32mreturn\u001b[0m \u001b[0minstance\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/usr/local/lib/python3.7/site-packages/tensortrade/exchanges/simulated/stochastic_exchange.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m     32\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     33\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0m__init__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 34\u001b[0;31m         \u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__init__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata_frame\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     35\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     36\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_base_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdefault\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'base_price'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/usr/local/lib/python3.7/site-packages/tensortrade/exchanges/simulated/simulated_exchange.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, data_frame, **kwargs)\u001b[0m\n\u001b[1;32m     58\u001b[0m             slippage_model, str) else slippage_model()\n\u001b[1;32m     59\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 60\u001b[0;31m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreset\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     61\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     62\u001b[0m     \u001b[0;34m@\u001b[0m\u001b[0mproperty\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/usr/local/lib/python3.7/site-packages/tensortrade/exchanges/simulated/stochastic_exchange.py\u001b[0m in \u001b[0;36mreset\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    181\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    182\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0mreset\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 183\u001b[0;31m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_generate_price_history\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m/usr/local/lib/python3.7/site-packages/tensortrade/exchanges/simulated/stochastic_exchange.py\u001b[0m in \u001b[0;36m_generate_price_history\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    144\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    145\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0m_generate_price_history\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 146\u001b[0;31m         \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_model_type\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'FBM'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    147\u001b[0m             \u001b[0mprice_fbm\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mFractionalBrownianMotion\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_times_to_generate\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhurst\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_hurst\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    148\u001b[0m             \u001b[0mprice_volatility\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mprice_fbm\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msample\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_times_to_generate\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mzero\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mAttributeError\u001b[0m: 'StochasticExchange' object has no attribute '_model_type'"
     ]
    }
   ],
   "source": [
    "exchange = StochasticExchange()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 204
    },
    "colab_type": "code",
    "id": "DTzQgIJ_TjP9",
    "outputId": "7cabdda9-b833-4603-b495-80b19f17df21"
   },
   "outputs": [],
   "source": [
    "exchange.data_frame.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 542
    },
    "colab_type": "code",
    "id": "7wdnCoc7YQfV",
    "outputId": "09c51455-f788-4c63-bcb3-d36ed141f79b"
   },
   "outputs": [],
   "source": [
    "plot_ohlcv(exchange.data_frame)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 542
    },
    "colab_type": "code",
    "id": "XCZycnOuY1jP",
    "outputId": "2f16c1e8-c101-4c6e-a795-e955f2391609",
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "plot_stochastic_processes(exchange.data_frame, \"Generated Price \")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Tune your own parmeters"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "J1NyIEeDbJ6f"
   },
   "source": [
    "####MODEL PARAMETERS\n",
    "The model parameters class contains all of the parameters used by the following stochastic processes. The parameters have been prefixed with the name of the stochastic process they are used in. Calibration of the stochastic processes would involve looking for the parameter values which best fit some historical data.\n",
    "*   `all_s0` This is the starting asset value\n",
    "*   `all_time` This is the amount of time to simulate for\n",
    "*   `all_delta` This is the delta, the rate of time e.g. 1/252 = daily, 1/12 = monthly\n",
    "*   `all_sigma` This is the volatility of the stochastic processes\n",
    "*   `gbm_mu` This is the annual drift factor for geometric brownian motion\n",
    "*   `jumps_lamda` This is the probability of a jump happening at each point in time\n",
    "*   `jumps_sigma` This is the volatility of the jump size\n",
    "*   `jumps_mu` This is the average jump size\n",
    "*   `cir_a` This is the rate of mean reversion for Cox Ingersoll Ross\n",
    "*   `cir_mu` This is the long run average interest rate for Cox Ingersoll Ross\n",
    "*   `all_r0` This is the starting interest rate value\n",
    "*   `cir_rho` This is the correlation between the wiener processes of the Heston model\n",
    "*   `ou_a` This is the rate of mean reversion for Ornstein Uhlenbeck\n",
    "*   `ou_mu` This is the long run average interest rate for Ornstein Uhlenbeck\n",
    "*   `sheston_a` This is the rate of mean reversion for volatility in the Heston model\n",
    "*   `heston_mu` This is the long run average volatility for the Heston model\n",
    "*   `heston_vol0` This is the starting volatility value for the Heston model\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "super_params = ModelParameters(\n",
    "                all_s0=1000,\n",
    "                all_r0=0.01,\n",
    "                all_time=6000,\n",
    "                all_delta=0.0001653439,\n",
    "                all_sigma=random.uniform(0.1, 0.8),\n",
    "                gbm_mu=random.uniform(-0.3, 0.6),\n",
    "                jumps_lamda=random.uniform(0.0071, 0.6),\n",
    "                jumps_sigma=random.uniform(-0.03, 0.04),\n",
    "                jumps_mu=random.uniform(-0.2, 0.2),\n",
    "                cir_a=3.0,\n",
    "                cir_mu=0.5,\n",
    "                cir_rho=0.5,\n",
    "                ou_a=3.0,\n",
    "                ou_mu=0.5,\n",
    "                heston_a=random.uniform(1, 5),\n",
    "                heston_mu=random.uniform(0.156, 0.693),\n",
    "                heston_vol0=0.06125\n",
    "            )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "params = {\n",
    "    'base_price': 1000,\n",
    "    'times_to_generate':100,\n",
    "    'model_type': \"HESTON\",\n",
    "    'param_type': \"Default\",\n",
    "    'timeframe': '1H',\n",
    "    'model_params':super_params,\n",
    "    \n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "exc = StochasticExchange(**params)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_ohlcv(exc.data_frame)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_stochastic_processes(exc.data_frame, \"Generated Price \")"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [],
   "name": "Stochastic_Exchange.ipynb",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
